import { ref, onMounted } from 'vue'

export function useBaiduMap() {
  const mapLoaded = ref(false)
  const mapInstance = ref(null)

  const loadBaiduMap = () => {
    return new Promise((resolve, reject) => {
      if (window.BMap) {
        mapLoaded.value = true
        resolve(window.BMap)
        return
      }

      const script = document.createElement('script')
      script.src = 'https://api.map.baidu.com/api?v=3.0&ak=您的密钥&callback=initBaiduMap'
      script.type = 'text/javascript'
      document.head.appendChild(script)

      window.initBaiduMap = () => {
        mapLoaded.value = true
        resolve(window.BMap)
      }

      script.onerror = reject
    })
  }

  return { mapLoaded, mapInstance, loadBaiduMap }
}